﻿using JESAI.Application.Abstractions.Admin;
using JESAI.Application.Abstractions.Dtos.Menu;
using JESAI.Application.Abstractions.Dtos;
using JESAI.Application.Service.Admin;
using JESAI.Framework.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using JESAI.Application.Abstractions.Admin.Dtos.Department;
using JESAI.Platform.WebApi.Vos.Admin;
using JESAI.Core.Util.Collections;
using JESAI.AutoMapper.ObjectMapping;

namespace JESAI.Platform.WebApi.Controllers.Admin
{
    [ApiController]
    [Authorize("permission")]
    [IgnoreResultHandlerAttribute]
    public class DepartmentController : ControllerBase
    {
        private readonly IDepartmentService _departmentService;

        public DepartmentController(IDepartmentService departmentService)
        {
            _departmentService = departmentService;
        }

        /// <summary>
        /// 设置部门信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Department/SetDepartment")]
        public async Task<BaseOutput> SetDepartment([FromBody] SetDepartmentInput input)
        {
            await _departmentService.UpdateOrInsert(input);

            return new BaseOutput { };
        }

        [HttpGet("/Department/DeleteDepartments")]
        public async Task<BaseOutput> DeleteDepartments(string[] ids)
        {
            await _departmentService.DeleteDepartments(ids);
            return new BaseOutput { };
        }

        [HttpGet("/Department/DeleteDepartment")]
        public async Task<BaseOutput> DeleteDepartment(string id)
        {
            await _departmentService.DeleteDepartment(id);
            return new BaseOutput { };
        }

        /// <summary>
        /// 查询平台菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Department/QueryAllDepartmentTrees")]
        public async Task<BaseOutput<List<DepartmentTreeResponse>>> QueryAllDepartmentTrees([FromQuery] QueryAllDepartmentInput input)
        {
            var list = await _departmentService.QueryAllDepartmentTrees(input);
            var treeList = list.ToTreeList(p => p.ParentId, p => p.Id, p => p.Children);

            return new BaseOutput<List<DepartmentTreeResponse>> { Data = treeList.MapTo<List<DepartmentTreeResponse>>() };
        }
    }
}
